<template>
  <div>
    <div class="search-content">
      <Col class="panel content-header" :span="24">
      <div class="title-bar cf">
        <h5 class="fl">
          <span>
            <a href="javascript:void(0);" class="link" @click="backToLast">
              <i class="fa fa-angle-left" aria-hidden="true"></i>返回上一页</a>
            <span class="dashboard-card-sep">|</span>
            <span class="dashboard-card-name"><span>周功率因数详情</span></span>
          </span>
        </h5>
      </div>
      </Col>
    </div>
    <div>
      <Row>
        <Col span="24" style="padding:10px;box-sizing:border-box;">
        <DatePicker style="width:240px;" type="daterange" :options="options" class="time-picker" v-model="timeDate" @on-change="onTimeChange" placement="bottom-end" placeholder="请选择日期..." show-week-numbers></DatePicker>
        </Col>
        <!-- 折线图 -->
        <Col span="24" class="panel map-panel" style="margin-bottom:15px;">
        <div style="width:100%;height:350px;" class="blank-chart">
          <div ref="chartRef" style="width: 100%;height: 100%;"></div>
        </div>
        </Col>
        <!-- 表格 -->
        <Col class="panel table-panel" :span="24" style="overflow-x: auto;margin-bottom: 20px;">
        <AnalTable v-if="tableData.length!=0" :tableData="tableData" :tableStyle="{ width:'100%' }"></AnalTable>
        </Col>
      </Row>
    </div>
  </div>
</template>
<script>
import AnalTable from "../AnalTable"
import mixin from "./mixin"
import { getDeviceListOfNew, getYinShu } from "@/api"
import utils from "@/utils/utils"

const { isBlankArray } = utils
export default {
  mixins: [mixin],
  components: { AnalTable },
  data() {
    return {
      echartInstence: null,
      tableData: []
    }
  },
  mounted() {
    this.initData()
  },
  methods: {
    onTimeChange() {
      const vm = this
      if (Array.isArray(vm.timeDate)) {
        const value = vm.timeDate
        if (isBlankArray(value)) {
          vm.timeDate = [
            vm
              .moment()
              .add(-6, "days")
              .format(),
            vm.moment().format()
          ]
        }
        const timeStart = vm.moment(vm.timeDate[0]),
          timeEnd = vm.moment(vm.timeDate[1]),
          days = timeEnd.diff(timeStart, "days")
        if (days === 7) {
          vm.initData()
        } else {
          vm.$Message.error("只能选择7天")
        }
      }
    },

    initData() {
      const vm = this,
        { id, name } = vm.$route.params,
        date = [
          ...vm.timeDate.map(t => vm.moment(t).format("YYYY-MM-DD"))
        ].join("~"),
        moment = vm.moment,
        params = {
          date,
          deviceNo: id
        }

      vm.echartInstence = vm.echarts.init(vm.$refs.chartRef)
      vm.echartInstence.showLoading({ text: "", color: "red" })
      getYinShu(params).then(res => {
        if (res.code !== 200 || JSON.stringify(res.data) === "{}") {
          vm.echartInstence.hideLoading()
          vm.echartInstence.clear()
          vm.tableData = []
          return false
        }
        this.printMap(res.data.list, date, name)
        let info = res.data.info
        try {
          vm.tableData = [
            {
              key: "A相最高功率因数",
              value: `${info["20"][0].value}(${moment(
                info["20"].createdAt
              ).format("YYYY-MM-DD HH:mm:ss")})`
            },
            {
              key: "A相最低功率因数",
              value: `${info["20"][1].value}(${moment(
                info["20"].createdAt
              ).format("YYYY-MM-DD HH:mm:ss")})`
            },
            {
              key: "B相最高功率因数",
              value: `${info["21"][0].value} (${moment(
                info["20"].createdAt
              ).format("YYYY-MM-DD HH:mm:ss")})`
            },
            {
              key: "B相最低功率因数",
              value: `${info["21"][1].value} (${moment(
                info["20"].createdAt
              ).format("YYYY-MM-DD HH:mm:ss")})`
            },
            {
              key: "C相最高功率因数",
              value: `${info["22"][0].value} (${moment(
                info["22"].createdAt
              ).format("YYYY-MM-DD HH:mm:ss")})`
            },
            {
              key: "C相最低功率因数",
              value: `${info["22"][1].value} (${moment(
                info["22"].createdAt
              ).format("YYYY-MM-DD HH:mm:ss")})`
            },
            {
              key: "总最高功率因数",
              value: `${info["23"][0].value} (${moment(
                info["23"].createdAt
              ).format("YYYY-MM-DD HH:mm:ss")})`
            },
            {
              key: "总最低功率因数",
              value: `${info["23"][1].value} (${moment(
                info["23"].createdAt
              ).format("YYYY-MM-DD HH:mm:ss")})`
            }
          ]
        } catch (e) {
          return false
        }
      })
    },

    printMap(dataArray = [], date, name) {
      const vm = this

      let xRay = dataArray[20].map(item => {
        return vm.moment(item.createdAt).format("HH:mm:ss")
      })
      let Amax = dataArray[20].map(item => {
        return item.value
      })

      let Bmax = dataArray[21].map(item => {
        return item.value
      })

      let Cmax = dataArray[22].map(item => {
        return item.value
      })

      let Zmax = dataArray[23].map(item => {
        return item.value
      })

      let option = {
        color: vm.color,
        title: {
          text: name,
          left: "center",
          subtext: date
        },
        tooltip: {
          trigger: "axis",
          axisPointer: {
            type: "cross",
            label: {
              backgroundColor: "#6a7985"
            }
          }
        },
        legend: {
          bottom: 0
        },
        grid: {
          top: "20%",
          left: "3%",
          right: "4%",
          bottom: "60",
          containLabel: true
        },
        xAxis: {
          type: "category",
          boundaryGap: false,
          data: xRay
        },
        yAxis: {
          type: "value"
        },
        series: [
          {
            name: "A相功率因数",
            type: "line",
            data: Amax
          },

          {
            name: "B相功率因数",
            type: "line",
            data: Bmax
          },

          {
            name: "C相功率因数",
            type: "line",
            data: Cmax
          },

          {
            name: "总功率因数",
            type: "line",
            data: Zmax
          }
        ]
      }

      vm.echartInstence.setOption(option)
      vm.echartInstence.hideLoading()
    },
    backToLast() {
      this.$router.go(-1)
    }
  }
}
</script>
